package neca.tmazo.vista.vistaGUI;

import java.awt.Color;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.SwingConstants;
import javax.swing.UIManager;

import neca.tmazo.datasets.Dataset;
import neca.tmazo.datasets.Song;
import neca.tmazo.modelo.WindowEventListener;
import neca.tmazo.vista.WindowItems;

/**
 * 
 * @author neca
 *
 */
public class VentanaCancion extends JPanel {
	private JLabel txtpnYear;
	private JTextField textField;

	private final static int STATE_DEFAULT=0;
	private final static int STATE_BOUGHT=1;
	private final static int STATE_PROPERTY=2;
	
	/**
	 * Crea el panel para la ventana de la cancion.
	 * @param observer 
	 */
	public VentanaCancion(final WindowEventListener observer, final Song song,boolean esComprada,boolean esPropiedad) {
		setBackground(Color.DARK_GRAY);
		
		int estado= esPropiedad ? STATE_PROPERTY : (esComprada ? STATE_BOUGHT : STATE_DEFAULT);
		int p = 25;
		//song.setPositiveVotes((int)(Math.random()*50));
		//song.setNegativeVotes((int)(Math.random()*50));

		
		setBounds(new Rectangle(0, 0, 750, 500));
		setLayout(null);
		JPanel panel = new JPanel();
		panel.setBackground(Color.DARK_GRAY);
		panel.setForeground(Color.DARK_GRAY);
		panel.setBounds(0, 0, 750, 75);
		add(panel);
		panel.setLayout(null);
		
		JTextPane txtpnJola = new JTextPane();
		txtpnJola.setForeground(Color.LIGHT_GRAY);
		txtpnJola.setFont(new Font("Dialog", Font.BOLD, 30));
		txtpnJola.setText(song.getTitle()+" - "+song.getAuthor());
		txtpnJola.setEditable(false);
		txtpnJola.setBackground(Color.DARK_GRAY);
		txtpnJola.setBounds(10, 19, 600, 45);
		panel.add(txtpnJola);
		
		JButton btnAtras = new JButton("Atr\u00E1s");
		btnAtras.setBackground(Color.DARK_GRAY);
		btnAtras.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				observer.onMenuItemSelected(WindowItems.CANCION_BOTON_ATRAS, null);
			}
		});
		btnAtras.setBounds(620, 21, 120, 40);
		panel.add(btnAtras);
		
		final JTextField infoPrecio = new JTextField();
		infoPrecio.setEditable(false);
		infoPrecio.setHorizontalAlignment(SwingConstants.RIGHT);
		infoPrecio.setFont(new Font("Tahoma", Font.PLAIN, 14));
		infoPrecio.setBounds(65, 420, 120, 40);
		add(infoPrecio);
		
		JButton btnComprar = new JButton();
		btnComprar.setBackground(Color.DARK_GRAY);
		btnComprar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				Dataset ds=new Dataset();
				ds.add(WindowItems.CANCION_FORMULARIO_PRECIO, infoPrecio.getText());
				//Devolvemos a modelo la eleccion del usuario y sus input
				observer.onMenuItemSelected(WindowItems.CANCION_BOTON_GUARDAR_O_DESCARGAR, ds);
				
			}
		});
		btnComprar.setBounds(280, 420, 120, 40);
		add(btnComprar);
		
		JPanel panel_info = new JPanel();
		panel_info.setBackground(Color.LIGHT_GRAY);
		panel_info.setBorder(null);
		panel_info.setBounds(30, 150, 405, 218);
		add(panel_info);
		panel_info.setLayout(null);
		
		JLabel txtpnAutor = new JLabel();
		txtpnAutor.setForeground(Color.DARK_GRAY);
		txtpnAutor.setBackground(UIManager.getColor("Panel.background"));
		txtpnAutor.setBounds(42, 27, 49, 20);
		panel_info.add(txtpnAutor);
		txtpnAutor.setFont(new Font("Tahoma", Font.BOLD, 13));
		txtpnAutor.setText("Autor:");
		
		JLabel infoAutor = new JLabel();
		infoAutor.setForeground(Color.DARK_GRAY);
		infoAutor.setBounds(101, 27, 260, 20);
		panel_info.add(infoAutor);
		infoAutor.setText(song.getAuthor());
		infoAutor.setFont(new Font("Tahoma", Font.PLAIN, 13));
		
		JLabel infoGen = new JLabel();
		infoGen.setForeground(Color.DARK_GRAY);
		infoGen.setBounds(101, 74, 260, 20);
		panel_info.add(infoGen);
		infoGen.setText(song.getGender());
		infoGen.setFont(new Font("Tahoma", Font.PLAIN, 13));
		
		JLabel txtpnGen = new JLabel();
		txtpnGen.setForeground(Color.DARK_GRAY);
		txtpnGen.setBounds(34, 74, 57, 20);
		panel_info.add(txtpnGen);
		txtpnGen.setText("Genero:");
		txtpnGen.setFont(new Font("Tahoma", Font.BOLD, 13));
		
		txtpnYear = new JLabel();
		txtpnYear.setForeground(Color.DARK_GRAY);
		txtpnYear.setBounds(54, 121, 37, 20);
		panel_info.add(txtpnYear);
		txtpnYear.setText("A\u00F1o:");
		txtpnYear.setFont(new Font("Tahoma", Font.BOLD, 13));
		
		JLabel infoYear = new JLabel();
		infoYear.setForeground(Color.DARK_GRAY);
		infoYear.setBounds(101, 121, 260, 20);
		panel_info.add(infoYear);
		infoYear.setText(""+song.getReleaseYear());
		infoYear.setFont(new Font("Tahoma", Font.PLAIN, 13));
		
		JLabel infoDuracion = new JLabel();
		infoDuracion.setForeground(Color.DARK_GRAY);
		infoDuracion.setBounds(101, 168, 260, 20);
		panel_info.add(infoDuracion);
		infoDuracion.setText(""+song.getDuration());
		infoDuracion.setFont(new Font("Tahoma", Font.PLAIN, 13));
		
		JLabel txtpnDuracion = new JLabel();
		txtpnDuracion.setForeground(Color.DARK_GRAY);
		txtpnDuracion.setBounds(21, 168, 70, 20);
		panel_info.add(txtpnDuracion);
		txtpnDuracion.setText("Duraci\u00F3n:");
		txtpnDuracion.setFont(new Font("Tahoma", Font.BOLD, 13));
		
		JPanel panel_opiniones = new JPanel();
		panel_opiniones.setBackground(Color.LIGHT_GRAY);
		panel_opiniones.setBounds(510, 281, 210, 87);
		add(panel_opiniones);
		panel_opiniones.setLayout(null);
		
		double pvotes = 0.50;
		
		if(song.getPositiveVotes()+song.getNegativeVotes()>0){
		pvotes = song.getPositiveVotes()/(double)(song.getPositiveVotes()+song.getNegativeVotes());
		}

		JLabel barra_v_positivos = new JLabel();
		barra_v_positivos.setOpaque(true);
		barra_v_positivos.setBackground(new Color(0, 128, 0));
		// Aqui es cuando se cambian las bounds, las originales serian (0,67,124,14). Si quieres cambiarlo solo hay que asegurarse de
		// multiplicar el campo 3 (longitud) por pvotes y hacer un cast a int.
		barra_v_positivos.setBounds(0, 73,(int)(210*pvotes), 14);
		panel_opiniones.add(barra_v_positivos);
		
		
		JLabel barra_v_negativos = new JLabel("");
		barra_v_negativos.setOpaque(true);
		barra_v_negativos.setBackground(new Color(178, 34, 34));
		barra_v_negativos.setBounds(0, 73, 210, 14);
		panel_opiniones.add(barra_v_negativos);
		
		JLabel infoVotosPos = new JLabel();
		infoVotosPos.setText(""+song.getPositiveVotes());
		infoVotosPos.setFont(new Font("Tahoma", Font.PLAIN, 13));
		infoVotosPos.setBounds(132, 11, 49, 20);
		panel_opiniones.add(infoVotosPos);
		
		JLabel infoVotosNeg = new JLabel();
		infoVotosNeg.setText(""+song.getNegativeVotes());
		infoVotosNeg.setFont(new Font("Tahoma", Font.PLAIN, 13));
		infoVotosNeg.setBounds(132, 42, 49, 20);
		panel_opiniones.add(infoVotosNeg);
		
		
		ImageIcon upVoteIcon = ImageManager.getImage(ImageManager.IMAGE_UP_VOTE);
		JLabel icono_votos_positivos = new JLabel(upVoteIcon);		
		icono_votos_positivos.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				//Devolvemos a modelo la eleccion del usuario y sus input
				observer.onMenuItemSelected(WindowItems.CANCION_BOTON_UPVOTE,null);
			}
		});
		icono_votos_positivos.setBounds(57, 11, 40, 20);
		panel_opiniones.add(icono_votos_positivos);
		
		ImageIcon downVoteIcon = ImageManager.getImage(ImageManager.IMAGE_DOWN_VOTE);
		JLabel icono_votos_negativos = new JLabel(downVoteIcon);
		icono_votos_negativos.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				//Devolvemos a modelo la eleccion del usuario y sus input
				observer.onMenuItemSelected(WindowItems.CANCION_BOTON_DOWNVOTE,null);
			}
		});
		icono_votos_negativos.setBounds(57, 42, 40, 20);
		panel_opiniones.add(icono_votos_negativos);

		JLabel jpanelMoney = new JLabel();
		jpanelMoney.setForeground(Color.LIGHT_GRAY);
		jpanelMoney.setText("\u20AC");
		jpanelMoney.setHorizontalAlignment(SwingConstants.RIGHT);
		jpanelMoney.setFont(new Font("Tahoma", Font.PLAIN, 18));
		jpanelMoney.setBounds(195, 428, 14, 20);
		add(jpanelMoney);
		
		JLabel label = new JLabel(ImageManager.getImage(ImageManager.IMAGE_LOGO_TMAZO_ALBUM_SONG));
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setBounds(510, 103, 210, 100);
		add(label);
		
		JLabel label_1 = new JLabel();
		label_1.setText("\u00BFTe ha gustado?");
		label_1.setHorizontalAlignment(SwingConstants.CENTER);
		label_1.setForeground(Color.LIGHT_GRAY);
		label_1.setFont(new Font("Tahoma", Font.PLAIN, 18));
		label_1.setBounds(510, 214, 210, 20);
		add(label_1);
		
		JLabel lblComparteTuOpinin = new JLabel();
		lblComparteTuOpinin.setText("Comparte tu opini\u00F3n con la comunidad Tmazo");
		lblComparteTuOpinin.setHorizontalAlignment(SwingConstants.RIGHT);
		lblComparteTuOpinin.setForeground(Color.LIGHT_GRAY);
		lblComparteTuOpinin.setFont(new Font("Tahoma", Font.PLAIN, 11));
		lblComparteTuOpinin.setBounds(506, 245, 216, 25);
		add(lblComparteTuOpinin);
		
		switch(estado)
		{
		case STATE_DEFAULT:
			btnComprar.setText("Comprar");
			if(song.getPrice()<0){
				infoPrecio.setText("No Disponible");
				jpanelMoney.setText("");
				btnComprar.setEnabled(false);
				btnComprar.removeAll();
			}else{
				infoPrecio.setText(song.getPrice()+"");
			}
			break;
		case STATE_BOUGHT:
			btnComprar.setText("Descargar");
			infoPrecio.setText("En Propiedad");
			jpanelMoney.setText("");
			break;
		case STATE_PROPERTY:
			btnComprar.setText("Actualizar");
			infoPrecio.setText(song.getPrice()+"");
			infoPrecio.setEditable(true);
			break;
		}
		
		
	}
}
